﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter21.Models.EventSamples
{
    using System.Threading;

    public static class Demo
    {
        public static void InvokeAll()
        {
            Action line = () =>
            {
                Console.ReadLine();
            };
            Invoke(MyManualResetEventTest, line);
        }
        public static void Invoke(params Action[] actions)
        {
            foreach (var action in actions)
            {
                action();
            }
        }
        public static void ManualResetEventClass()
        {
            const int taskCount = 4;
            var manualEvents = new ManualResetEventSlim[taskCount];
            var waitHandles = new WaitHandle[taskCount];
            var calces = new Calculator[taskCount];
            for (int i = 0; i < taskCount; i++)
            {
                int i1 = i;
                manualEvents[i] = new ManualResetEventSlim(false);
                waitHandles[i] = manualEvents[i].WaitHandle;
                calces[i] = new Calculator(manualEvents[i]);
                Task.Run(() => calces[i1].Calculation(i1 + 1, i1 + 3));

            }
            for (int i = 0; i < taskCount; i++)
            {
                int index = WaitHandle.WaitAny(waitHandles);
                if (index == WaitHandle.WaitTimeout)
                {
                    Console.WriteLine("超时!");
                }
                else
                {
                    manualEvents[index].Reset();
                    Console.WriteLine("{0} 的任务完成, 结果: {1}", index, calces[index].Result);
                }
            }
        }
        public static void CountdownEventClass()
        {
            const int taskCount = 4;
            var countdownEvent = new CountdownEvent(taskCount);
            var calces = new Calculator[taskCount];
            for (int i = 0; i < taskCount; i++)
            {
                int i1 = i;
                calces[i] = new Calculator(countdownEvent);
                Task.Run(() => calces[i1].Calculation(i1 + 1, i1 + 3));
            }
            countdownEvent.Wait();
            Console.WriteLine("所有已完成");
            for (int i = 0; i < taskCount; i++)
            {
                Console.WriteLine("{0} 的任务, 结果: {1}", i, calces[i].Result);
            }
        }
        public static void MyManualResetEventTest()
        {
            const int taskCount = 4;
            var manualEventMain = new ManualResetEventSlim(false);
            var manualEvents = new List<ManualResetEventSlim>(taskCount + 1);
            var waitHandles = new List<WaitHandle>(taskCount);
            var runners = new List<Runner>(taskCount);
            for (int i = 0; i < taskCount; i++)
            {
                manualEvents.Add(new ManualResetEventSlim(false));
                waitHandles.Add(manualEvents[i].WaitHandle);
                runners.Add(new Runner(manualEvents[i], manualEventMain));
                int i1 = i;
                Task.Run(() => runners[i1].Run());
            }
            //WaitHandle.WaitAll(waitHandles.ToArray());
            Console.ReadLine();
            for (int i = 0; i < taskCount; i++)
            {
                manualEvents[i].Set();
                manualEventMain.Wait();
            }
        }
        public static void MyCountdownEventTest()
        {
            const int taskCount = 8;
            var countdownEvent = new CountdownEvent(taskCount);
            var runners = new List<Runner>(taskCount);
            for (int i = 0; i < taskCount; i++)
            {
                runners.Add(new Runner(countdownEvent));
                Task.Run(() => runners[Interlocked.Increment(ref i)].Run());
            }
        }
    }
}
